package com.dykj.apps.web.sysmanager.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.dykj.apps.web.sysmanager.dao.IGroupDAO;
import com.dykj.apps.web.sysmanager.dao.po.SysGroup;
import com.dykj.apps.web.sysmanager.dto.GroupInfoDTO;
import com.dykj.platform.framework.exception.ApplicationException;
import com.dykj.platform.framework.exception.DatabaseException;
import com.dykj.platform.framework.utils.BeanUtils;

public class GroupInfoBSImpl implements IGroupInfoBS {
	private IGroupDAO dao;
/**
 * 获取树形菜单
 */
	public List getGroupInfo(String parentid) throws ApplicationException {
		List<GroupInfoDTO> group = new ArrayList<GroupInfoDTO>();
		try {
			List list = dao.findGroupInfoById(parentid);
			for(int i=0;i<list.size();i++){
				SysGroup gro = (SysGroup)list.get(i);
				GroupInfoDTO dto = new GroupInfoDTO();
				BeanUtils.copyProperties(dto,gro);
				if(dao.getGroupCout(gro.getGroupid())> 0 ){
					dto.setIsfolder("true");
				}
				group.add(dto);
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}		
		return group;
	}
	
	public List getManagerGroupInfo(String parentid) throws ApplicationException {
		List<GroupInfoDTO> group = new ArrayList<GroupInfoDTO>();
		try {					
			List list = dao.findGroupInfoById(parentid);
			for(int i=0;i<list.size();i++){
				SysGroup gro = (SysGroup)list.get(i);
				GroupInfoDTO dto = new GroupInfoDTO();
				BeanUtils.copyProperties(dto,gro);
				if(dao.getGroupCout(gro.getGroupid())> 0 ){
					dto.setIsfolder("true");
				}
				dto.setHref("findGroupInfo!modifyGroupInfo.action?groupid="+gro.getGroupid());
				group.add(dto);
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}
	return group;
	}
	
	
	public GroupInfoDTO getGroupInfoDTO(String id) throws ApplicationException {
		GroupInfoDTO dto=new GroupInfoDTO();
		try {
			if(id!=null){
				SysGroup gro = (SysGroup)dao.getObject(SysGroup.class, id);
				if(gro!=null){
					BeanUtils.copyProperties(dto,gro);
				}
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}
		return dto;
	}
	public int delGroupInfo(String id) throws ApplicationException {
		try {
			if(dao.getGroupCout(id)>0){
				return -1;
			}else if(dao.findHaveUser(id).size()>0){
				return -2;
			}else{
				dao.delByKey(SysGroup.class, id);
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}
		return 0;
	}

	public int saveOrUpdateGroupInfo(GroupInfoDTO dto)
			throws ApplicationException {
		try {
			if(dto.getParentid()==null||"".equals(dto.getParentid())){
				dto.setGlevel("1");
			}else{
				SysGroup parentgro = (SysGroup) dao.getObject(SysGroup.class, dto.getParentid());
				int level = Integer.parseInt(parentgro.getGlevel())+1;
				dto.setGlevel(level+"");
			}
			
			if(dto.getGroupid()==null || "".equals(dto.getGroupid())){
				dto.setGroupid(dao.getKeyIndex("groupid", "sysgroup"));
				SysGroup gro = new SysGroup();
				BeanUtils.copyProperties(gro, dto);
				dao.save(gro);
			}else{
				//修改前先修改其他org关联表字段。
				SysGroup sourcegroup = (SysGroup)dao.getObject(SysGroup.class, dto.getGroupid());
				String thisOrg = dto.getOrg();
				String sourceOrg = sourcegroup.getOrg();
				//是否修改了org编号
				if(!sourceOrg.equals(thisOrg)){
					//机构修改时, 批量修改 经办机构编号
					dao.updateSysConOrgToThis(sourceOrg, thisOrg);
				}
				BeanUtils.copyProperties(sourcegroup, dto);
				dao.update(sourcegroup);
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}
		return 0;
	}
	/**
	 * 获得下拉列表map
	 */
	public Map getGroupMap(String id) throws ApplicationException {
		//使用LinkedHashMap可以决定迭代顺序
		Map<String, String> groupMap = new LinkedHashMap<String, String>();
		groupMap.put("", "无上级机构");
		try {
			if(id==null || "".equals(id)){
				id="1";
			}
			List list = dao.findAllParent(id); 
			for(int i=0;i<list.size();i++){				
				SysGroup gro = (SysGroup)list.get(i);
				GroupInfoDTO dto = new GroupInfoDTO();
				BeanUtils.copyProperties(dto,gro);
				groupMap.put(dto.getGroupid(), dto.getName()+"("+dto.getOrg()+", "+dto.getGlevel()+"级机构)");
			   }	
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}
		return groupMap;
	}
	
/**
 * get set 方法
 * @return
 */
	public IGroupDAO getDao() {
		return dao;
	}

	public void setDao(IGroupDAO dao) {
		this.dao = dao;
	}
	
	

}
